package com.common.config.user;

import com.vo.UserVO;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yz
 * @data: 2022/4/7 21:50 星期四
 * @file : UserByCookie.java
 */
public class UserByCookie {


    /**
     * 获取 用信息
     *
     * @param userTicket
     * @param request
     * @param response
     * @return
     */

    public static UserVO getUserByCookie(String tokenRedis, String userTicket, HttpServletRequest request, HttpServletResponse response, RedisTemplate redisTemplate) {
//        if (StringUtils.isEmpty(userTicket)) {
//            return null;
//        }

        //获取redis里的用户信息
//        Object obj =  redisTemplate.opsForValue().get("user:" + userTicket);
        Object obj = redisTemplate.opsForValue().get("Token:" + tokenRedis);
        if (obj == null) {
            throw new RuntimeException("用户不存在");
        }
        UserVO userVO = (UserVO) obj;

//        System.out.println("userVO = ======" + userVO);

        //如果没有找到用户 ，将用户信息写入cookie
        if (userVO == null) {
//            CookieUtil.setCookie(request, response, "userTicket", userTicket);
        }
        return userVO;
    }

}
